package com.chinasofti.java8.demo08;

import java.lang.reflect.Constructor;

public class ReflectDemo2 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        //method1();
        //method2();
        //method3();
        //返回单个构造方法
        Class clazz = Class.forName("com.chinasofti.java8.demo08.Student");
        Constructor constructor = clazz.getDeclaredConstructor(String.class);
        System.out.println(constructor);
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
        //返回单个公共构造方法
        Class clazz = Class.forName("com.chinasofti.java8.demo08.Student");
        Constructor constructor1 = clazz.getConstructor();
        System.out.println(constructor1);

        Constructor constructor2 = clazz.getConstructor(String.class,int.class);
        System.out.println(constructor2);
//如果没有对应的构造方法会报异常 NoSuchMethodException
        // Constructor constructor3 = clazz.getConstructor(int.class);
        // System.out.println(constructor3);
    }

    private static void method2() throws ClassNotFoundException {
        //获取全部构造方法列表
        Class clazz = Class.forName("com.chinasofti.java8.demo08.Student");
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor constructor :constructors){
            System.out.println(constructor);
        }
    }

    private static void method1() throws ClassNotFoundException {
        //获取class对象
        Class clazz = Class.forName("com.chinasofti.java8.demo08.Student");
        //获取构造方法,返回所有公共方法对象的数组
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor constructor :constructors){
            System.out.println(constructor);
        }
    }
}